package cn.com.demo.util;

import cn.com.demo.common.vo.BaseRepVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ResponseUtil {
    public static void ok(HttpServletResponse response){
        out(response,BaseRepVo.SUCCESS);
    }
    public static void error(HttpServletResponse response){
        out(response,BaseRepVo.FAIL);
    }
    public static void error(HttpServletResponse response,String message){
        BaseRepVo vo = BaseRepVo.FAIL;
        vo.setMsg(message);
        out(response,vo);
    }
    public static void out(HttpServletResponse response,BaseRepVo repVo){
        response.setStatus(HttpStatus.OK.value());
        response.setContentType("application/json;charset=UTF-8");
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            objectMapper.writeValue(response.getOutputStream(),repVo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
